#include "except.h"
#include "../PRINT/my_printf.h"
#include "../UART/uart.h"
#include "../TEST/test.h"

void except_und(){
    unsigned int und_addr;

    __asm__(
        "stmdb sp!, {r0-r12, lr}\n"
        "mov %0, lr\n"
        :"=r"(und_addr)
        );   // 保存现场，lr存的是产生undef异常的指令的下一条指令地址；
    printf("[%x] Triggered undefined exceptions!\r\n", und_addr - 4);
    asm("ldmia sp!, {r0-r12, pc}^\n");  // 恢复现场，'^'表示恢复CPSR；
}

void except_swi(){
    unsigned int swi_addr, val = 0;
    __asm__(
        "stmdb sp!, {r0-r12, lr}\n"
        "mov %0, lr\n"              // 异常指令的下一条指令地址
        "ldr r0, [lr, #-4]\n"
        "bic r0, r0, #0xFF000000\n"
        "mov %1, r0\n"              // 取出24bit中断号
        :"=r"(swi_addr), "=r"(val)
        );   // 保存现场
    swi_addr -= 4;      // 异常指令地址
    print_cpsr();       // 打印cpsr
    printf("[%x] Triggered SWI exceptions 0x%x\r\n", swi_addr, val);
    asm("ldmia sp!, {r0-r12, pc}^\n");  // 恢复现场，'^'表示恢复CPSR；
}